home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / c / library / bcfamily / source / dossleep.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-12  |  1.5 KB  |  72 lines

  1. //
  2. //      *******************************************************************
  3. //        JdeBP C++ Library Routines          General Public Licence v1.00
  4. //            Copyright (c) 1991,1992     Jonathan de Boyne Pollard
  5. //      *******************************************************************
  6. //
  7. // Part of FamAPI.LIB
  8. //
  9.  
  10. #include "famapi.h"
  11. #include "dosdos.h"
  12.  
  13. //
  14. //    Read the value of timer 0, faking an ascending counter
  15. //
  16. static unsigned short
  17. timer    ( void )
  18. {
  19.     disable() ;
  20.     outportb (0x43, 0) ;
  21.     int val = ~(inportb (0x40) + ((unsigned short)inportb(0x40) << 8)) ;
  22.     enable() ;
  23.     return val ;
  24. }
  25.  
  26. //
  27. //    Check the timer for mode 2 or mode 3 and return the delay multiplier
  28. //
  29. //    Mode 3 decrements by 2, so the timer will never have an odd value
  30. //
  31. static unsigned short
  32. init_timer    ( void )
  33. {
  34.     int i ;
  35.  
  36.     for (i = 0; i < UCHAR_MAX; i++) {
  37.         if (~timer() & 1)
  38.             return 1193U ;
  39.     }
  40.  
  41.     return 1193U << 1 ;
  42. }
  43.  
  44. //
  45. //    wait a specific interval
  46. //
  47. void _APICALL
  48. DosDosSleep    ( unsigned short milliseconds )
  49. {
  50.     static unsigned short multiplier = 0UL ;
  51.  
  52.     if (!multiplier) multiplier = init_timer() ;
  53.  
  54.     unsigned short then = timer() ;
  55.     unsigned short now ;
  56.     unsigned long when = then ;
  57.  
  58.     // Do not do long multiplication because Borland C++ does not support
  59.     // that inline, using library calls instead.
  60.  
  61.     while (milliseconds--) when += multiplier ;
  62.  
  63.     while ((now = timer()) < when) {
  64.         if (now < then) {
  65.             if (when > USHRT_MAX)
  66.                 when -= USHRT_MAX + 1UL ;
  67.             else
  68.                 break ;
  69.         }
  70.         then = now;
  71.     }
  72. }